package com.itheima.meta_handler;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Component
public class MyMetaObjectHandler implements MetaObjectHandler {
    @Override
    public void insertFill(MetaObject metaObject) {
        Object created = getFieldValByName("created", metaObject);
        if (ObjectUtil.isNull(created)){
            metaObject.setValue("created", new Date());
        }

        Object updated = getFieldValByName("updated", metaObject);
        if (ObjectUtil.isNull(updated)){
            metaObject.setValue("updated", new Date());
        }
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        Object updated = getFieldValByName("updated", metaObject);
        if (ObjectUtil.isNull(updated)){
            metaObject.setValue("updated", new Date());
        }
    }
}
